import 'dart:developer';
import 'package:get/get.dart';
import 'package:sports_new/entity/home/subs_list_entity.dart';
import 'package:sports_new/server/api/other_api.dart';
import 'package:sports_new/server/api/pay_api.dart';
import '../../entity/pay/recharge_entity.dart';
import '../../server/api/coupon_api.dart';
import '../../server/api/expert_api.dart';
import '../../utils/routes.dart';
import '../../utils/toast_utils.dart';
import '../../utils/user.dart';
import '../../utils/utils.dart';
import '../../widgets/pay_bottom_sheet.dart';

class SubsDetailLogic extends GetxController{
  int _priceIndex = 0;
  int? roomId = Get.parameters['roomId'].toInt();
  List<String> benefitIcons = ["subs_icon_1.png","subs_icon_2.png","subs_icon_3.png"];
  List<String> benefitNames = ["精选比赛","方案免费","超高性价比"];
  List<String> benefits = ["热门比赛优质解析","每日足篮方案免费看","每月最低可省888球币"];
  String? tip;
  SubsListEntity? entity;
  int get priceIndex => _priceIndex;

  set priceIndex(int value) {
    _priceIndex = value;
    update();
  }

  @override
  void onReady() {
    requestData();
    super.onReady();
  }

  Future toPay(String price,int type) async{
    Utils.onEvent('zjdy_ljzf');
    var x = double.parse(price.split(RegExp(r"[\u4e00-\u9fa5]")).join());
    double money = x;
    final coupons = await CouponApi.use1Coupons(
        gold: money,scene: PayScene.subs, otherId: '',roomId: roomId);
    final PayResult? result = await Get.bottomSheet(
      isScrollControlled: true,
      PayBottomSheet(
        payInfo: PayInfo(
          price: money.toString(),
          payScene: PayScene.subs,
          buyId: "${entity?.expertId ?? " "}-$type",
          coupons: coupons
        )
      )
    );
    if(result != null && result.toPay == PayStatus.goldPay){
      log("${result.toPay}");
      await PayApi.subsOrder(money, entity?.expertId ?? "",
        seq: DateTime.now().microsecondsSinceEpoch.toString(),
        subsType: type,
        couponId: result.coupon?.id,
        couponGold: result.coupon?.gold,
        roomId: roomId
      );
      Utils.onEvent('pay_done');
      if(Get.previousRoute == Routes.userPage){
        Get.back();
      }else{
        Get.offAndToNamed(
          Routes.userPage,
          arguments: entity?.expertId,
          parameters: {"type":"2","index":"dingyue"}
        );
      }
    }else if(result?.toPay == PayStatus.directPay){
      Utils.onEvent('dyxq_zfyd_qrzf');
      if(Get.previousRoute == Routes.userPage){
        Get.back();
      }else{
        Get.offAndToNamed(
            Routes.userPage,
            arguments: entity?.expertId,
            parameters: {"type":"2","index":"dingyue"}
        );
      }
    }else{
      Utils.onEvent('dyxq_zfyd_qxzf');
      ToastUtils.show("购买失败");
    }
    User.fetchUserInfo(fetchFocus: false);
  }

  Future requestData() async{
    entity = await ExpertApi.getSubsDetail(Get.arguments,roomId: roomId);
    if(entity?.subsPrices?[2].min != null) benefits[2] = "每月${entity?.subsPrices?[2].min}";
    var tips = await OtherApi.getAppList("app_res_tips");
    try {
      tip = tips
          ?.firstWhere((element) => element.title == "subs_detail")
          .content;
    }catch(e){
      log("no subs_detail");
    }
    update();
  }
}